//
//  tieZiDetailsViewController.m
//  SXHomeTownClub
//
//  Created by 晋商行 on 2018/5/29.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "tieZiDetailsViewController.h"
#import "tieZiXiangQingHeader.h"
#import "myPaiXuButtonView.h"
#import "HDAlertView.h"
#import "tieZiTableViewCell.h"
#import "tieZiTwoTableViewCell.h"
#import "tieZiXiangQingModel.h"

@interface tieZiDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    tieZiXiangQingHeader *tieZiHeader;
    myPaiXuButtonView *paiXuButtonView;
    NSInteger page;
    NSMutableArray *dataSouce;
}

@end

@implementation tieZiDetailsViewController

#pragma mark --初始化
- (void)addSubViews{
    self.navigationItem.title =@"山西老乡";
    self.myTableView.delegate =self;
    self.myTableView.dataSource =self;
    self.needNoTableViewDataTips =YES;
    self.myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    dataSouce =[NSMutableArray array];
    page =1;
    self.baseTableview =self.myTableView;
    [self initHeader];
}

- (void)initHeader{
    tieZiHeader =[[NSBundle mainBundle] loadNibNamed:@"tieZiXiangQingHeader" owner:nil options:nil].lastObject;
    UIView *vv =[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 195)];
    vv.backgroundColor =[UIColor redColor];
    [vv addSubview:tieZiHeader];
    [tieZiHeader.isjoin addTarget:self action:@selector(isJoinClick:) forControlEvents:UIControlEventTouchUpInside];
    tieZiHeader.sd_layout.topSpaceToView(vv, 0).bottomSpaceToView(vv, 0).rightSpaceToView(vv, 0).leftSpaceToView(vv, 0);
    self.myTableView.tableHeaderView =vv;
}
//数据
- (void)getInitData{
    [self requestHeaderData:self.myFID andUID:@"3"];
    [self requestListData:self.myFID andOrder:@"" andPage:[NSString stringWithFormat:@"%ld",page]];
    
}

- (void)requestListData:(NSString *)FID andOrder:(NSString *)order andPage:(NSString *)pp{
    NSMutableDictionary *params =[[NSMutableDictionary alloc] init];
    [params setObject:FID forKey:@"fid"];
    if (order.length > 0) {
        [params setObject:order forKey:@"order"];
    }else{
        [params setObject:@"dateline" forKey:@"order"];
    }
    [params setObject:pp forKey:@"page"];
    
    
    [JSXHttpTool Post:[NSString stringWithFormat:@"%s/api/Quan/forumlist",JSXInterface] params:params success:^(id json) {
        self->dataSouce =[tieZiXiangQingModel mj_objectArrayWithKeyValuesArray:json];
        [self.myTableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark --UITableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    tieZiXiangQingModel *model =dataSouce[indexPath.row];
    if (model.thumb.count < 2) {
       return 130;
    }else{
        CGFloat width =(kScreenWidth-40) / 3;
        if (model.thumb.count < 4) {
            return 84 + 0.7 * width;
        }else if (model.thumb.count < 7){
            return 84 + 0.7 * width * 2;
        }else{
           return 84 + 0.7 * width * 3;
        }
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    tieZiXiangQingModel *model =dataSouce[indexPath.row];
    if (model.thumb.count < 2) {
        static NSString *identifter =@"quanZiTableViewCell";
        tieZiTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifter];
        if (!cell) {
            cell =[[NSBundle mainBundle] loadNibNamed:@"tieZiTableViewCell" owner:nil options:nil].lastObject;
        }
        tieZiXiangQingModel *model =dataSouce[indexPath.row];
        cell.cellName.text =model.subject;
        cell.cellYueDu.text =[NSString stringWithFormat:@"%ld阅读",(long)model.views];
        cell.cellTime.text =model.date;
        if (model.thumb.count == 1) {
            [cell.cellImage sd_setImageWithURL:[NSURL URLWithString:model.thumb[0]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Group]];
        }else{
            
        }
        
        return cell;
    }else{
        static NSString *identifter =@"tieZiTwoTableViewCell";
        tieZiTwoTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifter];
        if (!cell) {
            cell =[[NSBundle mainBundle] loadNibNamed:@"tieZiTwoTableViewCell" owner:nil options:nil].lastObject;
        }
        tieZiXiangQingModel *model =dataSouce[indexPath.row];
        cell.cellName.text =model.subject;
        cell.cellYueDuLiang.text =[NSString stringWithFormat:@"%ld阅读",(long)model.views];
        cell.cellTime.text =model.date;
        cell.thumb =model.thumb;
        cell.pic =model.pic;
        
        NSNotification *notification =[NSNotification notificationWithName:@"thumb" object:nil userInfo:@{@"thumb":model.thumb,@"pic":model.pic}];
        //通过通知中心发送通知
        [[NSNotificationCenter defaultCenter] postNotification:notification];
        
        return cell;
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];
    tieZiXiangQingModel *model =dataSouce[indexPath.row];
    [self pushToPostDetail:model.tid anduid:@"3" andfrom_type:@"2"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    paiXuButtonView =[[NSBundle mainBundle] loadNibNamed:@"myPaiXuButtonView" owner:nil options:nil].lastObject;
    [paiXuButtonView.paiXuButton addTarget:self action:@selector(paiXuClick:) forControlEvents:UIControlEventTouchUpInside];
    return paiXuButtonView;
}
- (void)paiXuClick:(UIButton *)sender{
    HDAlertView *alertView = [HDAlertView showActionSheetWithTitle:@"类别"];
    [alertView addButtonWithTitle:@"最新" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        [self->paiXuButtonView.paiXuButton setTitle:@"最 新" forState:UIControlStateNormal];
    }];
    [alertView addButtonWithTitle:@"热门" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        [self->paiXuButtonView.paiXuButton setTitle:@"热 门" forState:UIControlStateNormal];
    }];
    [alertView addButtonWithTitle:@"取消" type:HDAlertViewButtonTypeCancel handler:^(HDAlertView *alertView) {
        
    }];
    [alertView show];
}


//头部数据请求
- (void)requestHeaderData:(NSString *)FID andUID:(NSString *)uid{
    NSMutableDictionary *params =[[NSMutableDictionary alloc] init];
    [params setObject:FID forKey:@"fid"];
    [params setObject:uid forKey:@"uid"];
    
    [JSXHttpTool Post:[NSString stringWithFormat:@"%s/api/Quan/foruminfo",JSXInterface] params:params success:^(id json) {
        NSLog(@"%@",json);
        if ([[[json objectForKey:@"isjoin"] stringValue] isEqualToString:@"0"]) {
            self->tieZiHeader.isjoin.selected =NO;
        }else{
            self->tieZiHeader.isjoin.selected =YES;
        }
        [self->tieZiHeader.headeImage sd_setImageWithURL:[NSURL URLWithString:[json objectForKey:@"icon"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
        self->tieZiHeader.headerName.text =[json objectForKey:@"name"];
        self->tieZiHeader.headerContent.text =[NSString stringWithFormat:@"帖子:%@",[json objectForKey:@"posts"]];
        [self->tieZiHeader.banZhuImage sd_setImageWithURL:[NSURL URLWithString:[[json objectForKey:@"admin"] objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
        self->tieZiHeader.banZhuName.text =[[json objectForKey:@"admin"] objectForKey:@"username"];
        
        
        NSArray *arr =[json objectForKey:@"member"];
        if (arr.count == 1) {
            NSDictionary *dic =arr[0];
            [self->tieZiHeader.userImageOne sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
            self->tieZiHeader.userImageTwo.hidden =YES;
            self->tieZiHeader.userImageThree.hidden =YES;
            if (IS_IPHONE_5) {
                self->tieZiHeader.userImageThree.hidden =YES;
            }
        }else if (arr.count == 2){
            for (int i = 0; i <2; i++) {
                NSDictionary *dic =arr[i];
                if (i == 0) {
                    [self->tieZiHeader.userImageOne sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
                }else if (i == 1){
                    [self->tieZiHeader.userImageTwo sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
                    self->tieZiHeader.userImageThree.hidden =YES;
                }
                self->tieZiHeader.userImageThree.hidden =YES;
                if (IS_IPHONE_5) {
                    self->tieZiHeader.userImageThree.hidden =YES;
                }
            }
            
        }else if (arr.count == 3){
            for (int i = 0; i <arr.count; i++) {
                NSDictionary *dic =arr[i];
                if (i == 0) {
                    [self->tieZiHeader.userImageOne sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
                }else if (i == 1){
                    [self->tieZiHeader.userImageTwo sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
                }else if (i == 2){
                    [self->tieZiHeader.userImageThree sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"headimg"]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
                }
                if (IS_IPHONE_5) {
                    self->tieZiHeader.userImageThree.hidden =YES;
                }
            }
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)isJoinClick:(UIButton *)sender{
    NSMutableDictionary *params =[[NSMutableDictionary alloc] init];
    [params setObject:self.myFID forKey:@"fid"];
    [params setObject:@(83) forKey:@"uid"];
    [JSXHttpTool Post:[NSString stringWithFormat:@"%s/api/Quan/join",JSXInterface] params:params success:^(id json) {
        if ([[json objectForKey:@"msg"] isEqualToString:@"加入成功"]) {
            self->tieZiHeader.isjoin.selected =YES;
        }else{
            self->tieZiHeader.isjoin.selected =NO;
        }
        [[TKAlertCenter defaultCenter] postAlertWithMessage:[json objectForKey:@"msg"]];
    } failure:^(NSError *error) {
        
    }];
}
@end
